home *** CD-ROM | disk | FTP | other *** search
-
- BAT-HINT # 10
-
- **************************************************************************
-
- from the BATHINTS library... part of the BATPOWER CONFERENCE from:
-
- THE PAINFRAME OPUS/FIDO 261/1004
-
- Baltimore, Maryland 1-301-488-7461
-
- **************************************************************************
-
- The IF subcommand
-
- The IF subcommand is not unique to batch file processing in DOS... the
- command may be issued from either a batch file or from the command line,
- though it finds its greatest use from within the batch file. Through the
- use of IF a user may check for several things:
-
- 1. whether a file exists
- 2. whether a specific errorlevel has been returned from the keyboard
- or from a program
- 3. whether a variable exists
- 4. the value of a variable or string
-
- The general syntax of the IF command is:
-
- if [not] {true statement} {perform command}
-
- where [not] is optional, {true statement} is a legal statement to test with
- the IF command and {perform command} is any executable command.
-
- Errorlevel Testing
- ------------------
-
- Many of you have no doubt already used the IF command to test for
- errorlevels, so lets start with a simple example of an errorlevel test:
-
- sample1.bat
- -----------
-
- echo off
- cls
- :start
- echo Press a key... (you must press the right one!)
- key
- if errorlevel 65 goto correct
- echo Sorry, that was the wrong key...
- goto start
- :correct
- echo You pressed uppercase A
- echo That was the letter I was waiting for!
-
- This example uses the KEY.COM utility available from BATPOWER.ARC, a
- utility that pauses the execution of a batch file and waits for a single
- key to be pressed at the keyboard... and then returns an errorlevel code
- equal to the scan code of the key pressed (all keys and key combinations
- have an associated scan code used by DOS; for details on the specific scan
- codes see BATHINT ? ?... bh_?.doc; for details on the use of KEY.COM see
- the doc file KEY.DOC packaged with the program in BATPOWER.ARC). In this
- example, the IF command is waiting for the uppercase (capital) A to be
- pressed... its scan code is 65. Lets examine the command in more detail:
-
- if errorlevel 65 goto correct
-
- We can divide the command statement into three parts according to the
- syntax already given. First, IF, well thats what we are talking about.
- Second, errorlevel 65, that is the {true statement} that we are testing...
- is the errorlevel 65, or more precisely, is the errorlevel equal to or
- greater than 65? Thats the way testing for errorlevels works. If a
- statement is made like:
-
- if errorlevel 65 {perform command}
-
- then the IF command tests whether the errorlevel returned is equal to or
- greater than the specified number. In sample1.bat we could narrow down the
- specificity of the test with a statement like:
-
- if errorlevel 65 if not errorlevel 66 {perform command}
-
- AhAh! A nested IF command. Examine closely what this statement says: If
- the errorlevel is 65 or greater AND if the errorlevel is NOT 66 or greater
- then {perform command}... so the errorlevel must be equal to 65 (or more)
- but not equal to 66 (or more)... thus the errorlevel MUST be 65 for the
- statement to be true. If the statement is true (if an A was pressed) then
- {perform command}, which in sample1.bat is GOTO CORRECT, is executed. If
- it is false (if the errorlevel is less than 65) then {perform command} is
- not executed and the batch file ignores {perform command} and jumps to the
- next line ( the line below). So with "if errorlevel XX if not errorlevel
- XX+1" we may single out a specific errorlevel for testing. Examining the
- third part of the statement "goto correct", we see the command to be
- executed IF THE STATEMENT IS TRUE. GOTO is batch file specific command in
- DOS which instructs DOS to jump to a specified label. In our example, the
- label is "correct" (though it could be anything, so long as that label
- exists). Notice that further down in the batch file is the following line:
-
- :correct
-
- and notice also that when specifying a label with the GOTO command it is
- not necessary to include the colon (:) before the label (but the label must
- have a colon where it actually appears as a directive in the batch file).
- So if everything is true (you actually did press the letter A), sample1.bat
- performs the "GOTO CORRECT" command and searches the batch file for the
- label :correct then continues executing each line, one at a time, from the
- label :correct. In our example, two echo commands are executed indicating
- that that was the right key to press... and the batch file ends. If the
- statement was FALSE (you did not press A; the errorlevel was less than and
- not equal to 65) then the batch file ignores the "goto correct" command and
- skips to the next line, which echos that you pressed the wrong key and then
- performs the "goto start" command, which jumps back to the :start label and
- asks you to press a key again (the letter A!).
-
- So great eh? What can I do with it? Well there are many applications,
- only one of which is a small menu of possible selections. As a last
- example, consider the following scenerio: You have three programs from
- which to choose and you have written a batch file which starts each
- application by its name. The applications are: Yorgi (a fanatstic game of
- fun and skill?!), RipWrite (a great word processor) and Snarf (a utility
- program that you cannot live without). Lets assume that each batch file
- (yorgi.bat, ripwrite.bat and snarf.bat) are available through the normal
- DOS command line by virtue of the fact that they are in the c:\bat
- subdirectory that is named in your path command... so that they be found
- when called upon). Rather than type the name of the batch file which
- starts these great programs, you have created a simple menu from which with
- a single keystroke you may select any one of them. That batch file may be
- written like this:
-
- menu.bat
- --------
-
- echo off
- cls
- echo Main Menu:
- echo 1. Yorgi
- echo 2. RipWrite
- echo 3. Snarf
- echo Press a number according to your selection...
- key
- if errorlevel 51 if not errorlevel 52 goto select3
- if errorlevel 50 if not errorlevel 51 goto select2
- if errorlevel 49 if not errorlevel 50 goto select1
- goto sorry
- :select1
- yorgi
- :select2
- ripwrite
- :select3
- snarf
- :sorry
- echo You must press a number between 1 and 3
- pause
- goto start
- cls
-
- Notice that the IF ERRORLEVEL statements are numbered backwards from the
- scan code for number 3 (51) to the scan code for number 1 (49). Following
- the logic given for sample1.bat, you should be able to see that you must
- start from the highest number down (if you have questions just ask!!!).
-
- Testing for the Presence of a File
- ----------------------------------
-
- IF can test for the existance of a file. Examine the following batch
- file:
-
- sample2.bat
- -----------
-
- echo off
- cls
- if exist myword.doc echo myword.doc exists!
- cls
-
- Simple enough... sample2.bat tests for the existance of a file called
- "myword.doc" and if the statement is true (that is, if myword.doc does
- exist), it executes the command "echo myword.doc exists!". If the file is
- not found the remainder of the command line is ignored and the batch file
- continues processing at the next line, which in this example clears the
- screen. Note that the "if exist" command only recognizes files in the
- current directory. To test for a file located elsewhere you should log
- onto the drive and into the subdirectory where the file is presumed to
- exist. For example, if the file was located on drive B in a subirectory
- called "document", you would need to change sample2.bat to read:
-
-
- echo off
- cls
- b:
- cd\document
- if exist b:\document\myword.doc echo myword.doc exists!
-
- Occasionally, the test for a specific file (as in sample2.bat) may be
- useful, but more commonly the filename is substituted for a REPLACEABLE
- PARAMETER that the user can specify when the batch file is initially called
- into action. Our example could be made more general by making the changes
- shown below:
-
- sample3.bat
- -----------
-
- echo off
- cls
- if exist %1 echo %1 exists!
- cls
-
- In this example, the %1 parameter would be entered at the command line when
- the batch file is started. For example, to test for the existance of a
- file called "another.doc" the user would start the batch file with the
- following command:
-
- sample3 another.doc
-
- The %0 parameter is always the name of the batch file (sample3 in this
- case) and "another.doc" is the %1 parameter. When the "if exist" line is
- executed, the term "another.doc" will be substituted for %1.
-
- More often than not, it is more useful to utilize a "goto" statement after
- the IF command. In sample2.bat and sample3.bat the "echo filename.ext!"
- message would only be flashed on the screen momentarily if the statement
- was true since the next and last line in these batch files issues the CLS
- (clear screen) command. A more refined way to write these batch files is
- shown below:
-
- sample4.bat
- -----------
-
- echo off
- cls
- if exist %1 goto foundit
- echo Sorry, %1 does not exist.
- pause
- goto fini
- :foundit
- echo %1 exists!
- pause
- :fini
- cls
-
- In this example, if %1 exists batch file processing jumps to the "foundit"
- label and echos that fact... then pauses for a keystroke before clearing
- the screen and terminating. If %1 does not exist it echos "Sorry...",
- waits for a keystroke and jumps to the "fini" label then clears the screen
- and terminates.
-
- At first glance, the usefullness of checking for the existance of a file
- doesn't seem to have much use... but consider this example:
-
- You normally store some of your programs as compressed archives to save on
- disk space. One such program, a game called "WASTER" is archived into a
- single file called "waster.arc" on drive C in the subdirectory GAMES. You
- have a batch file that starts the program by unarchiving the program onto a
- large RAM disk (say drive H), then runs the game (waster.exe) from the RAM
- drive. Here is a batch file that might do such processing... and also
- check to see if waster.arc is already unarchived on drive H... and if so,
- does not try to unarc it again, but rather justs runs the program from the
- RAM drive. This example uses the program PKXARC.EXE from PKWARE to do the
- unarchiving:
-
- waster.bat
- ----------
-
- echo off
- cls
- if not exist h:\waster.exe pkxarc c:\games\waster.arc h:\
- h:
- waster
-
- Equivalence Testing
- -------------------
-
- The correct syntax for testing equivalence is:
-
- if stringA==stringB {perform command}
-
- where stringA and stringB are ASCII characters and may be substituted for
- replaceable parameters and/or environmental variables. Very often one may
- wish to include a parameter after the name of a batch file so as to direct
- some special function. For example, password.bat tests for the presence of
- a password given as the %1 replaceable parameter. In this example, the
- password is "eyesonly":
-
- password.bat
- ------------
-
- echo off
- cls
- if %1==eyesonly goto correct
- echo Your password was incorrect.
- goto end
- :correct
- {perform some commands here}
- :end
-
- In order that the command that you would place on line 7 to be invoked, you
- would have to enter the following statement at the command line:
-
- password eyesonly
-
- Nothing else would work. If the incorrect parameter is given, or if no
- parameter was given, the IF statement would not be true and the file would
- display "Your password was incorrect". Of course, such a batch file would
- not provide any substantial level of security, since a user would only need
- to view the batch file with an editor or with the type command to see what
- the correct password should be. Note that the password must match exactly.
- If the command was given as:
-
- password EYESONLY
- or
- password eysOnly
-
- or any other variation of upper and lower case letters, the IF statement
- would be false.
-
- Often it is of benefit to test for the absence of a parameter, especially
- when you may have forgotten the correct syntax for a complicated batch file
- written some time ago. Note the following statement:
-
- if "%1"=="" {perform command}
-
- This statement will be true when the %1 parameter is a NULL string... the
- %1 parameter was not given on the command line which started the batch
- file. Note that the NULL string is a real string... it is just a string
- with a size of zero. If the quotation marks were left out:
-
- if %1=="" {perform command}
-
- a syntax error message would be generated. The quotation marks can just as
- easily be replaced with dollar signs, exclamation marks... almost anything.
- As an example several statements are shown below. If no parameter is given
- they are all true, if a parameter was given, they are all false, and one
- works just as well as any of the others:
-
- if "%1"=="" {perform command}
- if $%1$==$$ {perform command}
- if !%1!==!! {perform command}
- if x%1==x {perform command}
- if %1x==x {perform command}
- if .%1==. {perform command}
-
- Use whichever style you prefer.
-
- Tests for equivalence may also employ environmental variables (see BATHINT?
- for a discussion of environmental variables). As always, the environmental
- variable must be enclosed within quotation marks for its value to be
- correctly substituted. For example, if the following command had been
- issued:
-
- set name=Bimbo
-
- so that the variable "name" contained the value "Bimbo", the value of the
- variable could be tested with the following statements:
-
- if %name%==Bimbo goto correct
- echo The name was not Bimbo.
- goto end
- :correct
- echo The name was Bimbo.
- :end
-
- *********************************************************** David Creasey
-